package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.BookListDao;
import person.aoao.library.dao.BorrowListDao;
import person.aoao.library.entity.BookList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BookListService;
import person.aoao.library.service.TypeListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (BookList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@Service("bookListService")
public class BookListServiceImpl implements BookListService {
    @Resource
    private BookListDao bookListDao;
    @Resource
    private TypeListService typeListService;

    @Resource
    private BorrowListDao borrowListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param bookid 主键
     * @return 实例对象
     */
    @Override
    public BookList queryById(Integer bookid) {
        BookList list=this.bookListDao.queryById(bookid);
        list.setBorrowCount(this.borrowListDao.getCountByBook(bookid));
        return list;
    }

    /**
     * 分页查询
     *
     * @param bookList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BookList> queryByPage(BookList bookList, PageRequest pageRequest) {
        long total = this.bookListDao.count(bookList);
        return new PageImpl<>(this.bookListDao.queryAllByLimit(bookList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param bookList 实例对象
     * @return 实例对象
     */
    @Override
    public BookList insert(BookList bookList) {
        this.bookListDao.insert(bookList);
        return bookList;
    }

    /**
     * 修改数据
     *
     * @param bookList 实例对象
     * @return 实例对象
     */
    @Override
    public BookList update(BookList bookList) {
        this.bookListDao.update(bookList);
        return this.queryById(bookList.getBookid());
    }

    /**
     * 通过主键删除数据
     *
     * @param bookid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer bookid) {
        return this.bookListDao.deleteById(bookid) > 0;
    }

    @Override
    public ResponseData getBookList(Integer num, Integer page) {
        ListData data=new ListData();
        List<BookList> list=this.bookListDao.getBookList(num,(page-1)*num);
        for(BookList item:list){
            item.setTypeList(this.typeListService.queryById(item.getBooktype()));
        }
        long count =this.bookListDao.getBookListCount();
        data.setCount(count);
        data.setData(list);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData searchBookList(String keyword, Integer type, Integer num, Integer page) {
        ListData data=new ListData();
        List<BookList> list=this.bookListDao.searchBookList("%"+keyword+"%",type,num,(page-1)*num);
        for(BookList item:list){
            item.setTypeList(this.typeListService.queryById(item.getBooktype()));
        }
        long count=this.bookListDao.searchCount("%"+keyword+"%",type);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getNavBook(Integer type, Integer num, Integer page) {
        List<BookList> list=this.bookListDao.getBookListByType(type,num,(page-1)*num);
        long count=this.bookListDao.getTypeCount(type);
        ListData data=new ListData();
        data.setCount(count);
        data.setData(list);
        return  new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getHotBooks() {
        List<BookList> books=this.bookListDao.getBookListAllInfo();
        for(BookList item:books){
            item.setBorrowCount(this.borrowListDao.getByBook(item.getBookid()).size());
            item.setTypeList(this.typeListService.queryById(item.getBooktype()));
        }
        Collections.sort(books);
        return new ResponseData(ResponseCode.SUCCESS,books.subList(0,10));
    }
}
